using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmartContentUI.Expedient
{
    public partial class Properties : UserControl
    {
        #region Variables
        private SmartContentBusiness.Session ses;
        SmartContentDB.Interfaces.Content content;
        private bool editMode;
        #endregion

        #region Delegates
        public delegate void Exit();
        public event Exit OnExit;
        #endregion

        #region Constructor
        public Properties(SmartContentBusiness.Session ses)
        {
            InitializeComponent();
            this.ses = ses;
            content = null;
            editMode = false;
        }
        #endregion

        public void loadData(int contentId)
        {
            content = ses.getFullContent(contentId);
            this.name_tb.Text = content.getName();
            this.desc_tb.Text = content.getDescription();
            if (content.getType() == SmartContentDB.Interfaces.Content.ContentType.File)
            {
                this.dir_tb.Visible = true;
                this.dir_lbl.Visible = true;
            }
            else
            {
                this.dir_tb.Visible = false;
                this.dir_lbl.Visible = false;
            }
            this.dir_tb.Text = content.getDirectory();
        }

        private void EnableDisableControls(bool save)
        {
            if (editMode)
            {
                this.name_tb.Enabled = true;
                this.desc_tb.Enabled = true;
                this.dir_tb.Enabled = true;
                this.commandBtn.Text = "Guardar";
                this.CancelBtn.Text = "Cancelar";
            }
            else
            {
                if (save)
                {
                    content.setDescription(this.desc_tb.Text);
                    content.setName(this.name_tb.Text);
                    content.setDirectory(this.dir_tb.Text);
                    this.ses.updContent(content);
                    this.OnExit();
                }
                this.name_tb.Enabled = false;
                this.desc_tb.Enabled = false;
                this.dir_tb.Enabled = false;
                this.commandBtn.Text = "Editar";
                this.CancelBtn.Text = "Salir";
            }
        }

        #region Events
        private void commandBtn_Click(object sender, EventArgs e)
        {
            editMode = !editMode;
            EnableDisableControls(true);
        }
        private void CancelBtn_Click(object sender, EventArgs e)
        {
            if (editMode == false)
            {
                OnExit();
            }
            else
            {
                editMode = false;
                EnableDisableControls(false);
            }
        }
        #endregion

    }
}
